#!/bin/bash
#-----------------------------------------------------------------------
#
# RUN-BUILD-VISIT - Run the build_visit script.
#
# Author: Eric Brugger
# Date:   May 17, 2022
#
# Usage:
#    run-build-visit -<machine name> -v <version> -s <build_visit_script>
#    Valid machine names:
#       poodle     (LC, Linux, x86_64, toss4),
#       lassen     (LC, Linux, power9),
#       rzwhippet  (LC, Linux, x86_64, toss4),
#       rzvernal   (LC, Linux, x86_64, toss4-cray),
#       crossroads (LANL, x86_64)
#       tuolumne   (LC, Linux, x86_64, toss4-cray),
#       rzadams    (LC, Linux, x86_64, toss4-cray)
#
# Modifications:
#    Eric Brugger, Fri Feb 17 09:36:29 PST 2023
#    Added support for building on poodle.
#
#    Eric Brugger, Wed Mar 15 13:45:39 PDT 2023
#    Added support for building libstripack on poodle.
#
#    Eric Brugger, Fri Jun  2 14:25:12 PDT 2023
#    Modified rztrona for toss4.
#
#    Eric Brugger, Tue Jul 18 16:30:02 PDT 2023
#    Added support for building on rzgenie.
#
#    Eric Brugger, Thu Jul 27 16:51:41 PDT 2023
#    Added support for building on magma.
#
#    Eric Brugger, Mon Nov  6 16:43:55 PST 2023
#    Updated poodle and lassen for building with VTK9 and Qt6. I
#    also removed quartz, rztrona, rzansel, magma, jade and sierra since
#    they are all duplicates of poodle and lassen.
#
#    Cyrus Harrison,Fri Jan 12 10:22:59 PST 2024
#    Removed rzgenie, added rzwhippet
#
#    Cyrus Harrison, Tue Feb 20 16:56:21 PST 2024
#    Various updates for lassen and toss4, removed stripack
# 
#    Justin Privitera, Thu Mar  7 14:44:01 PST 2024
#    Changed /usr/workspace/wsa/visit/visit/... to
#    /usr/workspace/visit/visit/... for poodle.
# 
#    Justin Privitera, Wed Mar 13 16:07:44 PDT 2024
#    Changed /usr/workspace/wsa/visit/visit/... to
#    /usr/workspace/visit/visit/... for lassen.
#
#    Eric Brugger, 05/22/24
#    Added support for building on rzvernal.
# 
#    Justin Privitera, Fri Sep  6 20:42:29 PDT 2024
#    Completely reworked this script to have no more duplication.
# 
#    Justin Privitera, Tue Sep 10 16:23:34 PDT 2024
#    Removed rztopaz.
#
#    Eric Brugger, Mon Oct  7 09:03:43 PDT 2024
#    Added support for building on tuolumne.
# 
#    Justin Privitera, Wed Nov  6 16:11:16 PST 2024
#    Added support for building on rzadams (secretly the same as 
#    tuolumne).
# 
#    Justin Privitera, Fri Nov 22 16:40:46 PST 2024
#    Removed kickit with extreme prejudice.
#
#    Eric Brugger, Wed Dec  4 16:00:35 PST 2024
#    Removed trinity and added crossroads.
#
#-----------------------------------------------------------------------

#
# Parse the execute line, providing default values for error checking.
#
poodle=false
lassen=false
rzgenie=false
rzvernal=false
crossroads=false
tuolumne=false

machine_name="unknown"

version=undefined
build_visit_script=undefined

ran_build_visit=false

#
# The loop is executed once for each symbol on the execute line. This means
# that $1 may be blank for later executions of the loop if any "shift 2"
# commands are executed. The variable abc is not used in the loop.  
#
for abc
do
   case $1 in
      -poodle)
         poodle=true
         machine_name="poodle"
         shift
         ;;
      -lassen)
         lassen=true
         machine_name="lassen"
         shift
         ;;
      -rzwhippet)
         rzwhippet=true
         machine_name="rzwhippet"
         shift
         ;;
      -rzvernal)
         rzvernal=true
         machine_name="rzvernal"
         shift
         ;;
      -crossroads)
         crossroads=true
         machine_name="crossroads"
         shift
         ;;
      -tuolumne)
         tuolumne=true
         machine_name="tuolumne"
         shift
         ;;
      -rzadams)
         tuolumne=true
         machine_name="rzadams"
         shift
         ;;
      -v)
         version=$2
         shift 2
         ;;
      -s)
         build_visit_script=$2
         shift 2
         ;;
   esac
done

#
# Check that the version and build_visit script was provided.
#
if [ "$build_visit_script" = "undefined" ] || [ "$version" = "undefined" ]
then
   echo "Usage: -<machine name> -v <version> -s <build_visit_script>"
   echo "Valid machine names:"
   echo "    poodle     (LC, Linux, x86_64, toss4),"
   echo "    lassen     (LC, Linux, power9),"
   echo "    rzwhippet  (LC, Linux, x86_64, toss4),"
   echo "    rzvernal   (LC, Linux, x86_64, toss4-cray),"
   echo "    crossroads (LANL, Linux, x86_64)"
   echo "    tuolumne   (LC, Linux, x86_64, toss4-cray),"
   echo "    rzadams    (LC, Linux, x86_64, toss4-cray)"
   exit
fi

#
# Check that the build_visit script exists.
#
if [ ! -f $build_visit_script ]
then
   echo "Build_visit script doesn't exist."
   exit
fi

#######################################################
############ MACHINE SPECIFIC INSTRUCTIONS ############
#######################################################

#
# Info and set ran_build_visit
#
if [ "$poodle" = true ] || [ "$lassen" = true ] || [ "$rzwhippet" = true ] || \
   [ "$rzvernal" = true ] || [ "$crossroads" = true ] || \
   [ "$tuolumne" = true ]; then
   echo "Building on $machine_name"
   ran_build_visit=true
else
   echo "case for $machine_name not implemented. Bailing out."
   exit
fi

#
# Set destination directory
#
if [ "$poodle" = true ] || [ "$rzwhippet" = true ]; then
   dest_dir=/usr/workspace/visit/visit/thirdparty_shared/$version/toss4
elif [ "$lassen" = true ]; then
   dest_dir=/usr/workspace/visit/visit/thirdparty_shared/$version/blueos
elif [ "$rzvernal" = true ]; then
   dest_dir=/usr/workspace/visit/visit/thirdparty_shared/$version/toss4-cray-gfx90a
elif [ "$tuolumne" = true ]; then
   dest_dir=/usr/workspace/visit/visit/thirdparty_shared/$version/toss4-cray-gfx942
elif [ "$crossroads" = true ]; then
   dest_dir=/usr/projects/views/visit/thirdparty_shared/$version
else
   echo "case for $machine_name not implemented. Bailing out."
   exit
fi

#
# Set destination group
#
if [ "$poodle" = true ] || [ "$lassen" = true ] || [ "$rzwhippet" = true ] || \
   [ "$rzvernal" = true ] || [ "$tuolumne" = true ]; then
   dest_group=visit
elif [ "$crossroads" = true ]; then
   dest_group=viz
else
   echo "case for $machine_name not implemented. Bailing out."
   exit
fi

#
# housekeeping
#
if [ "$poodle" = true ] || [ "$lassen" = true ] || [ "$rzwhippet" = true ] || \
   [ "$rzvernal" = true ] || [ "$crossroads" = true ] || \
   [ "$tuolumne" = true ]; then
   if [ -f ${build_visit_script}_log.${machine_name} ]; then
      mv ${build_visit_script}_log.${machine_name} ${build_visit_script}_log
   fi
   if [ ! -d ${dest_dir} ]; then
      mkdir -p ${dest_dir}
   fi
else
   echo "case for $machine_name not implemented. Bailing out."
   exit
fi

#
# Call build_visit
#
if [ "$poodle" = true ]; then
   env PAR_COMPILER=/usr/tce/packages/mvapich2/mvapich2-2.3.6-gcc-10.3.1/bin/mpicc \
       PAR_COMPILER_CXX=/usr/tce/packages/mvapich2/mvapich2-2.3.6-gcc-10.3.1/bin/mpicxx \
       PAR_INCLUDE=-I/usr/tce/packages/mvapich2/mvapich2-2.3.6-gcc-10.3.1/include \
       ./$build_visit_script --group ${dest_group} --required --optional --mesagl --uintah --parallel \
       --qt6 --qwt --no-pyside --no-visit --makeflags -j16 --thirdparty-path ${dest_dir}
elif [ "$rzwhippet" = true ]; then
   env PAR_COMPILER=/usr/tce/packages/mvapich2/mvapich2-2.3.7-gcc-10.3.1/bin/mpicc \
       PAR_COMPILER_CXX=/usr/tce/packages/mvapich2/mvapich2-2.3.7-gcc-10.3.1/bin/mpicxx \
       PAR_INCLUDE=-I/usr/tce/packages/mvapich2/mvapich2-2.3.7-gcc-10.3.1/include \
       ./$build_visit_script --group ${dest_group} --required --optional --mesagl --uintah --parallel \
        --qt6 --qwt --no-pyside --no-gdal --no-visit --makeflags -j20 \
        --thirdparty-path ${dest_dir}
elif [ "$lassen" = true ]; then
   module load gcc/8.3.1
   env PAR_COMPILER=/usr/tce/packages/spectrum-mpi/spectrum-mpi-rolling-release-gcc-8.3.1/bin/mpicc \
       PAR_COMPILER_CXX=/usr/tce/packages/spectrum-mpi/spectrum-mpi-rolling-release-gcc-8.3.1/bin/mpicxx \
       PAR_INCLUDE=-I/usr/tce/packages/spectrum-mpi/ibm/spectrum-mpi-rolling-release/include \
       ./${build_visit_script} --group ${dest_group} --required --optional --mesagl --uintah \
       --no-gdal --no-openexr --no-embree --no-ispc --no-tbb --no-pidx \
       --no-ospray --parallel --qt --qwt --no-pyside --no-visit --no-boost --makeflags -j16 \
       --thirdparty-path ${dest_dir}
elif [ "$rzvernal" = true ]; then
   env PAR_COMPILER=/usr/tce/packages/cray-mpich/cray-mpich-8.1.28-gcc-12.2.1-magic/bin/mpicc \
       PAR_COMPILER_CXX=/usr/tce/packages/cray-mpich/cray-mpich-8.1.28-gcc-12.2.1-magic/bin/mpicxx \
       PAR_INCLUDE=-I/usr/tce/packages/cray-mpich/cray-mpich-8.1.28-gcc-12.2.1-magic/include \
       ./$build_visit_script --group ${dest_group} --required --optional --mesagl --uintah --parallel \
       --qt6 --qwt --no-pyside --no-gdal --no-uintah --no-visit --makeflags -j24 --thirdparty-path ${dest_dir} 
elif [ "$crossroads" = true ]; then
   env C_COMPILER=gcc \
       CXX_COMPILER=g++ \
       PAR_COMPILER=mpicc \
       PAR_COMPILER_CXX=mpic++ \
       PAR_INCLUDE=-I/opt/cray/pe/mpich/8.1.28/ofi/intel/2022.1/include \
       PAR_LIBS="-L/opt/cray/pe/mpich/8.1.28/ofi/intel/2022.1/lib -Wl,-rpath=/opt/cray/pe/mpich/8.1.28/ofi/intel/intel/lib" \
       ./$build_visit_script --group ${dest_group} --required --optional --mesagl --parallel \
       --qt6 --qwt --no-adios --no-gdal --no-pyside --no-visit \
       --makeflags -j16 --thirdparty-path ${dest_dir}
elif [ "$tuolumne" = true ]; then
   env CC=gcc \
       CXX=g++ \
       PAR_COMPILER=cc \
       PAR_COMPILER_CXX=CC \
       ./$build_visit_script --group ${dest_group} --required --optional --mesagl --parallel --no-moab --no-icet \
       --qt6 --qwt --no-pyside --no-gdal --no-uintah --no-pidx --no-visit --makeflags -j24 --thirdparty-path ${dest_dir} 
else
   echo "case for $machine_name not implemented. Bailing out."
   exit
fi

#
# Final housekeeping and permissions
#
if [ "$poodle" = true ] || [ "$lassen" = true ] || [ "$rzwhippet" = true ] || \
   [ "$rzvernal" = true ] || [ "$crossroads" = true ] || \
   [ "$tuolumne" = true ]; then
   mv ${build_visit_script}_log ${build_visit_script}_log.${machine_name}
   # just in case perms
   echo "changing permissions of ${dest_dir}"
   chmod -R ug+w,a+rX ${dest_dir}
   chgrp -R ${dest_group} ${dest_dir}
else
   echo "case for $machine_name not implemented. Bailing out."
   exit
fi

#
# Check that build_visit was run.
#
if [ "$ran_build_visit" = "false" ]
then
   echo "Invalid or missing machine name."
   echo "Usage: -<machine name> -v <version> -s <build_visit_script>"
   echo "Valid machine names:"
   echo "    poodle     (LC, Linux, x86_64, toss4),"
   echo "    lassen     (LC, Linux, power9),"
   echo "    rzgenie    (LC, Linux, x86_64, toss3),"
   echo "    rzvernal   (LC, Linux, x86_64, toss4-cray),"
   echo "    crossroads (LANL, Linux, x86_64)"
   echo "    tuolumne   (LC, Linux, x86_64, toss4-cray),"
   echo "    rzadams    (LC, Linux, x86_64, toss4-cray),"
   exit
fi
